package com.tjetc.config;

import com.tjetc.common.handlers.JsonListTypeHandler;
import com.tjetc.common.handlers.SpotTagsTypeHandler;
import com.tjetc.dao.TagsMapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.List;

@Configuration
 // 根据你的 Mapper 包路径调整
public class MyBatisConfig {

     @Autowired
     private TagsMapper tagsMapper;

     @PostConstruct
     public void init() {
         // 手动设置 TagsMapper
         SpotTagsTypeHandler.setTagsMapper(tagsMapper);
     }
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);

        // 注册自定义 TypeHandler
        sqlSessionFactoryBean.getObject().getConfiguration().getTypeHandlerRegistry()
                .register(List.class, JdbcType.VARCHAR, JsonListTypeHandler.class);

        return sqlSessionFactoryBean.getObject();
    }
}
